package com.atguigu.gulimall.gulimallmember.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.entity.SocialUser;
import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.gulimall.gulimallmember.exception.PhoneExistException;
import com.atguigu.gulimall.gulimallmember.exception.UsernameExistException;
import com.atguigu.gulimall.gulimallmember.feign.CouponFeignService;
import com.atguigu.gulimall.gulimallmember.vo.MemberLoginVo;
import com.atguigu.gulimall.gulimallmember.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.gulimallmember.entity.MemberEntity;
import com.atguigu.gulimall.gulimallmember.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author zhishi
 * @email zhishi@gmail.com
 * @date 2023-05-03 14:29:52
 */
@RestController
@RequestMapping("gulimallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/test")
    public R test() {
        R test = couponFeignService.test();
        return R.ok().put("couple", test.get("couple"));
    }

    @PostMapping("/regist")
    public R regist(@RequestBody RegisterVo registerVo) {
        try {
            memberService.regist(registerVo);
        } catch (PhoneExistException phoneExistException) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException usernameExistException) {
            return R.error(BizCodeEnum.USERNAME_EXIST_EXCEPTION.getCode(), BizCodeEnum.USERNAME_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (memberEntity == null) {
            return R.error(BizCodeEnum.LOGINCAT_OR_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.LOGINCAT_OR_PASSWORD_INVALID_EXCEPTION.getMsg());
        } else {
            return R.ok();
        }
    }

    @PostMapping("/social/login")
    public R socialLogin(@RequestBody SocialUser socialUser) {
        MemberEntity memberEntity = memberService.login(socialUser);
        if (memberEntity == null) {
            return R.error(BizCodeEnum.SOCIAL_LOGIN_INVALID.getCode(), BizCodeEnum.SOCIAL_LOGIN_INVALID.getMsg());
        } else {
            return R.ok().put("data", memberEntity);
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
